﻿using Mes.Pendant.UnitMeasures.Dto;
using Mes.Pendant.UnitMeasures;
using Mes.Pendant.UnitMeasureW;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services;
using Mes.Pendant.WarehousesettingW;

using System.Linq.Dynamic.Core;
using Volo.Abp.Domain.Repositories;
using Mes.Pendant.Warehousesettings.Dto;
using Mes.Pendant.WarehousesettingServicess;
using Volo.Abp.ObjectMapping;
using Mes.Pendant.Common.DtoMapping;

namespace Mes.Pendant.WarehousesettingsService
{
	public class WarehousesettingSerVice : CrudAppService<WarehousesettingW.Warehousesettings, WarehousesettingDto, int, PagedResultRequestDto, WarehousesettingDto>, IWarehousesettings
    {
        private readonly IRepository<WarehousesettingW.Warehousesettings, int> _repository;
        public WarehousesettingSerVice(IRepository<WarehousesettingW.Warehousesettings, int> repository) : base(repository)
        {
            _repository = repository;
        }
      
        /// <summary>
        /// 显示查询分页
        /// </summary>
        /// <param name="codes"></param>
        /// <param name="name"></param>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        public async Task<PagedResult> WarehousesettingList(string? codes, string? name, int page, int limit)
        {
            var predicate = PredicateBuilder.New<WarehousesettingW.Warehousesettings>(true);
            if (!string.IsNullOrWhiteSpace(codes))
            {
                predicate=predicate.And(m=>m.WarehouseCode == codes);
            }
            if (!string.IsNullOrWhiteSpace(name))
            {
                predicate = predicate.And(m => m.WarehouseName == name);
            }
            var wearList = _repository.GetListAsync(predicate).Result;

            PagedResult pagedResult=new PagedResult();
            pagedResult.CurrentPage= page;
            pagedResult.PageSize= limit;
            pagedResult.RowCount = wearList.Count();
            pagedResult.PageCount = (int)Math.Ceiling((pagedResult.RowCount) / (double)limit);
            pagedResult.Queryable=wearList.OrderBy(m=>m.Id).Skip((page-1) * limit).Take(limit).AsQueryable();
            pagedResult.Queryable= pagedResult.Queryable.MapToList<WarehousesettingDto>().AsQueryable();
            return pagedResult;
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="warehousesettingDto"></param>
        /// <returns></returns>
        public async Task<int> CreateWarehousesetting(WarehousesettingDto warehousesettingDto)
        {
            var wareIsExist = _repository.AnyAsync(m => m.WarehouseCode == warehousesettingDto.WarehouseCode).Result;
            if (wareIsExist)
            {
                return -1;
            }

            var wareModel = AutoMapperExt.MapTo<WarehousesettingW.Warehousesettings>(warehousesettingDto);
            await _repository.InsertAsync(wareModel);
            return warehousesettingDto.Id;
        }
        
        /// <summary>
        /// 编辑
        /// </summary>
        /// <param name="warehousesettingDto"></param>
        /// <returns></returns>
        public async Task<int> UpdateWarehousesetting(WarehousesettingDto warehousesettingDto)
        {
            var wareIsExist = _repository.AnyAsync(m => m.WarehouseCode == warehousesettingDto.WarehouseCode).Result;
            if (wareIsExist)
            {
                return -1;
            }

            var wareModel = AutoMapperExt.MapTo<WarehousesettingW.Warehousesettings>(warehousesettingDto);
            await _repository.UpdateAsync(wareModel);
            return warehousesettingDto.Id;
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<int> DeleteWarehousesetting(int id)
        {
            _repository.DeleteAsync(id).Wait();
            return id;
        }

        /// <summary>
        /// 根据id查询列表
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<WarehousesettingDto> GetWarehousesettingById(int id)
        {
            WarehousesettingW.Warehousesettings warehousesettings = _repository.GetAsync(id).Result;
            WarehousesettingDto warehousesettingDto = ObjectMapper.Map<WarehousesettingW.Warehousesettings, WarehousesettingDto>(warehousesettings);
            return warehousesettingDto;
        }
    }
}
